import {USER_STATE} from "../common/enums"
import http from '../common/request'
import UNI_APP from '@/.env.js'
const baseUrl = UNI_APP.BASE_URL
export default {
	state: {
		userInfo: {},
		isLogin:false,
		state: USER_STATE.FREE 
	},

	mutations: {
		setUserInfo(state, userInfo) {
			
			// 使用深拷贝方式，否则小程序页面不刷新
			Object.assign(state.userInfo, userInfo); 
		console.log("mutatiionn:setUserInfo",state.userInfo)
		},
		setIsLogin(state,isLogin){
				state.isLogin = isLogin
		},
		setUserState(state, userState) {
			state.state = userState;
		},
		clear(state){
			state.userInfo = {};
			state.state = USER_STATE.FREE;
		}
	},
	actions:{
		loadUser(context){
			return new Promise((resolve, reject) => {
				http({
					url: baseUrl + '/user/self',
					method: 'GET'
				}).then((userInfo) => {
					
					console.log("loadUser:",userInfo)
					context.commit("setUserInfo",userInfo);
					context.commit("setIsLogin",true);
					resolve();
				}).catch((res)=>{
					console.log("获取自己数据失败")
					reject(res);
				});
			})
		},
		
	}
	
}